package com.zw.pdm.module.report.service;

import com.zw.pdm.entity.craft.CraMakeDetail;
import com.zw.pdm.entity.designtask.DsnProStruct;
import com.zw.pdm.entity.rep.Change;
import com.zw.pdm.entity.rep.Plan;
import com.zw.pdm.entity.rep.Sheet;
import com.zw.pdm.entity.rep.VerifyProcess;
import com.zw.pdm.module.report.mapper.ChangeMapper;
import com.zw.pdm.module.report.mapper.PlanMapper;
import com.zw.pdm.module.report.mapper.SheetMapper;
import com.zw.pdm.module.report.mapper.VerifyProcessMapper;
import com.zw.pdm.module.report.model.Project;
import com.zw.pdm.module.system.service.LogService;
import com.zw.pdm.util.CommonUtils;
import com.zw.pdm.util.FileUtils;
import com.zw.pdm.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


/**
 * @Description:
 *
 * @Author: China.sgl
 * @Date: 2019/12/6 08:42
 */
@Service
@SuppressWarnings("all")
public class SheetService {

    @Autowired
    private SheetMapper sheetMapper;
    @Autowired
    private PlanMapper planMapper;
    @Autowired
    private VerifyProcessMapper verifyProcessMapper;
    @Autowired
    private ChangeMapper changeMapper;

    @Value("${user.upload.path}")
    private String uploadPath;
    @Autowired
    private LogService logService;

    /** 按ID查询计划单 */
    public Sheet getSheetById(Integer id) {
        return sheetMapper.getOne(id);
    }

    /** 按 计划单ID 查计划 */
    public List<Plan> findPlanBySheetId(Integer sheetId) {
        return planMapper.findBySheetId(sheetId);
    }

    /** 按 变更单ID 查询变更计划 */
    public List<Change> findChangeBySheetId(Integer sheetId) {
        return changeMapper.findBySheetId(sheetId);
    }

    /** 查询计划单所有审核流程 */
    public List<VerifyProcess> findVpBySheetId(Integer sheetId) {
        return verifyProcessMapper.findVpBySheetId(sheetId);
    }

    /** 上传临时文件 */
    public String uploadTemp(MultipartFile file) {
        if (null != file) {
            StringBuffer sb = new StringBuffer();
            sb.append(uploadPath);
            sb.append(ShiroUtils.getLoginName());
            sb.append("/temp/");
            String fileName = FileUtils.upload(file, sb.toString());
            if (CommonUtils.isNotNull(fileName)) {
                logService.saveLog("上传附件", "名称：" + fileName);
                return sb.toString() + fileName;
            }
        }
        return null;
    }

    /** 按项目编号查询项目信息 */
    public List<Project> findProjectByCode(String code) {
        return sheetMapper.findProjectByCode(code);
    }

    /** 根据项目号查询部分 */
    public List<DsnProStruct> findPartByProjectCode(String code, String type) {
        return sheetMapper.findPartByProjectCode(code, type);
    }

    /** 根据项目号和部分名查询图号 */
    public List<CraMakeDetail> findDrawNoByProCodeAndPart(String proCode, Integer partId) {
        return sheetMapper.findDrawNoByProCodeAndPart(proCode, partId);
    }

    /** 根据项目号和部分名查询图号 */
    public List<CraMakeDetail> findDrawNoByProCodeAndPartAndId(String proCode, Integer partId, Integer id) {
        return sheetMapper.findDrawNoByProCodeAndPartAndId(proCode, partId, id);
    }

    /** 查图号 */
    public CraMakeDetail findDrawNoById(Integer id) {
        return sheetMapper.findDrawNoById(id);
    }
}
